package logic

import (
	"context"
	"errors"
	"shop/model/mysql"

	"shop/internal/svc"
	"shop/shop"

	"github.com/zeromicro/go-zero/core/logx"
)

type HousePriceLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHousePriceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HousePriceLogic {
	return &HousePriceLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HousePriceLogic) HousePrice(in *shop.HousePriceRequest) (*shop.HousePriceResponse, error) {
	// todo: add your logic here and delete this line
	var hs []mysql.House
	mysql.DB.Find(&hs)
	if in.PriceMin > 1000000 {
		logx.Error("最小值应该大于一百万")
		return nil, errors.New("最小值应该大于一百万")
	}
	if in.PriceMax < 3000000 {
		logx.Error("最大值应该小于三百万")
		return nil, errors.New("最大值应该小于三百万")
	}
	if in.PriceMin > in.PriceMax {
		logx.Error("最小值不能小于最大值")
		return nil, errors.New("最小值不能小于最大值")
	}

	var housePrice []*shop.HousePrice
	for _, h := range hs {
		housePrice = append(housePrice, &shop.HousePrice{
			Image: h.Image,
			Title: h.Title,
			Price: float32(h.Price),
			Num:   int64(h.Num),
			Info:  h.Info,
		})
	}
	return &shop.HousePriceResponse{
		List: housePrice,
	}, nil
}
